package file

import (
	"hft/Http/Models/request"
	adminreq "hft/Http/Models/request/admin_req"
	"hft/Http/Models/response/admin_res"
	"hft/Http/Services/file"
	utils "hft/Http/Utils"
	"hft/e"
	"hft/tools"
	"io"
	"net/http"
	"strconv"

	fileSql "hft/Database/admin/file"

	"github.com/gin-gonic/gin"
)

type File struct{}

// 上传封面
func (*File) UploadTempFile(c *gin.Context) {

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage(e.InValidToken, c)
	}

	var fileInfo *request.TempFile

	c.ShouldBindJSON(&fileInfo)

	resp, err := http.Get(fileInfo.Url) //下载文件

	if err != nil {
		utils.FailWithMessage("文件转存失败", c)
		resp.Body.Close()
		return
	}
	err = file.FileService.UploadTempFile(claims.BaseClaims, io.Reader(resp.Body), fileInfo.ID)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)
}

func (*File) Delete(c *gin.Context) {

	var ids *request.IDS

	c.ShouldBindJSON(&ids)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage(e.InValidToken, c)
		return
	}

	err = file.FileService.Delete(claims.BaseClaims, ids.IDS)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)

}

func (*File) Edit(c *gin.Context) {
	var data *fileSql.File

	c.ShouldBindJSON(&data)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage(e.InValidToken, c)
		return
	}

	err = file.FileService.Edit(claims.BaseClaims, int(data.ID), data.RealName)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)

}

func (*File) List(c *gin.Context) {

	var data adminreq.FilePage

	c.ShouldBind(&data)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage(e.InValidToken, c)
		return
	}

	list, err := file.FileService.List(claims.BaseClaims, &data)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(list, c)

}

func (*File) ReturnUUID(c *gin.Context) {

	var list []*adminreq.FileUUID

	c.ShouldBindJSON(&list)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage(e.InValidToken, c)
		return
	}

	newlist, err := file.FileService.ReturnUUID(claims.BaseClaims, list)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(newlist, c)

}

// 文件上传
func (*File) UploadFile(c *gin.Context) {

	var err error

	pid, _ := strconv.Atoi(c.Query("pid"))

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage(e.InValidToken, c)
		return
	}

	newFile, header, _ := c.Request.FormFile("file")
	if err != nil {
		c.String(500, "上传出错")
	}

	chunkTotal, _ := strconv.Atoi(c.Request.FormValue("chunkTotal"))
	filename := c.Request.FormValue("filename")
	uuid := c.Request.FormValue("uuid")
	fileType := c.Request.FormValue("type")
	index, _ := strconv.Atoi(c.Request.FormValue("index"))
	size, _ := strconv.Atoi(c.Request.FormValue("size"))

	var res *admin_res.FileProgress

	if chunkTotal == 1 {

		res, err = file.FileService.UploadSingleFile(claims.BaseClaims, newFile, filename, uuid, pid, fileType, int64(size))

	} else if chunkTotal > 1 {

		// 分片上传
		res, err = file.FileService.UploadSliceFile(claims.BaseClaims, newFile, chunkTotal, filename, uuid, pid, index, fileType, header.Size, int64(size))

	}

	if err != nil {
		utils.OkWithMessage(e.UploadFail, c)
		return
	}

	utils.OkWithData(res, c)

}

var FileController = new(File)
